<?php
/**
 * Ver. PHP 5
 * @package: browser checks functions
 * @author: Web Analytics
 * @date: 03/01/2011
 */

/* ------------------------------------------------------------------------- */

/** 
 * Browser Check
 *
 * @desc identifies browser out of user-agent string
 * @access public
 * @param string http_user_agent : user agent string from $_SERVER array	 
 * @param boolean showPics : true if thumbnails should be returned, else strings
 * @return short string or thumbnail naming user agent
 */	 
function browser_check( $http_user_agent, $showPics ) 
{
	// number of browsers detected
	$browserNr = 27;
	// return string if no browser is detected
	$defaultBrowser = "no browser data";
	
	// array with detection signatures
	$detectionArray[ 0 ] = "Firefox";
	$detectionArray[ 1 ] = "MSIE 7.";
	$detectionArray[ 2 ] = "Opera";
	$detectionArray[ 3 ] = "Msnbot";
	$detectionArray[ 4 ] = "MSIE";      // all other versions of MSIE
	$detectionArray[ 5 ] = "Konqueror";
	$detectionArray[ 6 ] = "Netscape";
	$detectionArray[ 7 ] = "Safari";
	$detectionArray[ 8 ] = "Googlebot";  
	$detectionArray[ 9 ] = "Yahoo!";
	$detectionArray[ 10 ] = "Mozilla";
	
	$detectionArray[ 11 ] = "VMBot";  	   // less famous crawler or search-tools
	$detectionArray[ 12 ] = "MaSagool";
	$detectionArray[ 13 ] = "sproose";  
	$detectionArray[ 14 ] = "Krugle";  
	$detectionArray[ 15 ] = "psbot";
	
	$detectionArray[ 16 ] = "ichiro";  
	$detectionArray[ 17 ] = "Nutch";
	
	$detectionArray[ 18 ] = "WISEnutbot";
	$detectionArray[ 19 ] = "Speedy Spider";  // entireweb.com
	
	$detectionArray[ 20 ] = "majestic12";
	$detectionArray[ 21 ] = "Shim-Crawler";
	$detectionArray[ 22 ] = "Exabot";
	$detectionArray[ 23 ] = "WeRelate";
	$detectionArray[ 24 ] = "SKIZZLE";
	$detectionArray[ 25 ] = "Guruji";
	$detectionArray[ 26 ] = "YahooSeeker";      
	$detectionArray[ 27 ] = "VoilaBot";     	
				
	// array with images to show for each browser
	$imgArray[ 0 ] = "firefox.png";
	$imgArray[ 1 ] = "Internetexplorer.gif";
	$imgArray[ 2 ] = "opera_.png";
	$imgArray[ 3 ] = "msn.png";
	$imgArray[ 4 ] = "inetexplorer.png";
	$imgArray[ 5 ] = "konqueror.png";
	$imgArray[ 6 ] = "netscape.gif";
	$imgArray[ 7 ] = "safari.png";
	$imgArray[ 8 ] = "google.png";
	$imgArray[ 9 ] = "yahoo.png";
	$imgArray[ 10 ]= "mozilla_ico.gif";
	
	$imgArray[ 11 ] = "vmlogo.gif";
	$imgArray[ 12 ] = "sagool.gif";
	$imgArray[ 13 ] = "sproose.gif";
	$imgArray[ 14 ] = "krugle.gif";
	$imgArray[ 15 ] = "picsearch.gif";
		
	$imgArray[ 16 ] = "goo.gif";
	$imgArray[ 17 ] = "nutch.gif";   
	$imgArray[ 18 ] = "wise_nut.gif";    
	$imgArray[ 19 ] = "speedyspider.gif";
	$imgArray[ 20 ] = "majestic12.png";
	//$imgArray[ 21 ] = "";     
	$imgArray[ 22 ] = "exalead.gif";
	$imgArray[ 23 ] = "weRelate.gif";
	$imgArray[ 24 ] = "Skizzle.gif";
	$imgArray[ 25 ] = "GurujiLogo.gif";
	$imgArray[ 26 ] = "yahoo.png";        // YahooSeeker
	$imgArray[ 27 ] = "voila.gif";       
	
	
	// array with String to show (instead of pictures)
	$strArray[ 0 ] = "Firefox";
	$strArray[ 1 ] = "MSIE";
	$strArray[ 2 ] = "Opera";
	$strArray[ 3 ] = "Msnbot";
	$strArray[ 4 ] = "MSIE";
	$strArray[ 5 ] = "Konqueror";
	$strArray[ 6 ] = "Netscape";
	$strArray[ 7 ] = "Safari";
	$strArray[ 8 ] = "Googlebot";
	$strArray[ 9 ] = "Yahoo Slurp";
	$strArray[ 10 ]= "Mozilla";
	
	$strArray[ 11 ]= "VMBot";
	$strArray[ 12 ]= "Sagool";
	$strArray[ 13 ]= "Sproose";
	$strArray[ 14 ]= "Krugle";
	$strArray[ 15 ]= "PSBot";
	
	$strArray[ 16 ]= "goo";
	$strArray[ 17 ]= "Nutch";
	
	$strArray[ 18 ]= "WISEnutbot";      
	$strArray[ 19 ]= "Speedy Spider";
	
	$strArray[ 20 ]= "PicSearch";      
	$strArray[ 21 ]= "Shim-Crawler";
	$strArray[ 22 ]= "Exabot";
	$strArray[ 23 ]= "WeRelate";
	$strArray[ 24 ]= "Skizzle";
	$strArray[ 25 ]= "Guruji";
	$strArray[ 26 ]= "YahooSeeker";
	$strArray[ 27 ]= "Voila";      
	
	for( $i = 0; $i < $browserNr; $i++ ) {    
		if ( stripos( $http_user_agent, $detectionArray[ $i ] ) !== false ) {
			$http_user_agent = htmlentities($http_user_agent);
			if ( $showPics && isset( $imgArray[ $i ] )){			
				$browserResultString = '<img src="'.SITEURL.'images/br/'.$imgArray[ $i ].'" alt="'.$http_user_agent.'" title="'.$strArray[ $i ].' - '.$http_user_agent.'" width="18" height="18" border="0" class="br_chk_img" />';
			}else{
			  $browserResultString = $strArray[ $i ];
			}
			
			//check whether the URL is a web spider
			if ( isWebSpider( $http_user_agent) ){
			  $browserResultString .= "<font size=\"-3\">BOT</font>";
			}  
			return isset( $browserResultString ) ? $browserResultString : $http_user_agent;
		}
	}
	
	// if none is detected return default
	if ( strcmp( $http_user_agent, "" ) == 0){
	  return $defaultBrowser;    
	}else{
	  return $http_user_agent;   
	}  
}

/* ------------------------------------------------------------------------- */

/** 
 * Search Engine Check
 *
 * @desc identifies search engine out of user-agent string
 * @access public
 * @param string http_user_agent : user agent string from $_SERVER array	 
 * @param boolean showPics : true if thumbnails should be returned, else strings
 * @return short string or thumbnail naming user agent
 */	 
function search_engine_check( $http_user_agent, $showPics ) 
{
	// number of browsers detected
	$browserNr = 20;
	// return string if no browser is detected
	$defaultBrowser = 0;
		
	// array with detection signatures
	$i = 0;
	$detectionArray[ $i++ ] = "google."; // famous crawler or search-tools
	$detectionArray[ $i++ ] = "aol.";
	$detectionArray[ $i++ ] = "bing.";
	$detectionArray[ $i++] = "msnbot.";	
	$detectionArray[ $i++ ] = "msn.";
	$detectionArray[ $i++ ] = "googlebot.";  
	$detectionArray[ $i++ ] = "yahoo.";
	
	$detectionArray[ $i++ ] = "VMBot.";  	   // less famous crawler or search-tools
	$detectionArray[ $i++ ] = "maSagool.";
	$detectionArray[ $i++ ] = "sproose.";  
	$detectionArray[ $i++ ] = "Krugle.";  
	$detectionArray[ $i++ ] = "psbot.";
	
	$detectionArray[ $i++ ] = "ichiro.";  // less famous crawler or search-tools
	$detectionArray[ $i++ ] = "nutch.";
	$detectionArray[ $i++ ] = "guruji.";
	$detectionArray[ $i++ ] = "yahooSeeker.";      
	$detectionArray[ $i++ ] = "voilaBot.";     
	
	$detectionArray[ $i++ ] = "lycos.";    //some old search tools 	
	$detectionArray[ $i++ ] = "altavista.";
	$detectionArray[ $i++ ] = "entireweb.";
				
	// array with images to show for each browser
	$i = 0;
	$imgArray[ $i++ ] = "google.png";
	$imgArray[ $i++ ] = "aol.png";
	$imgArray[ $i++ ] = "bing.png";
	$imgArray[ $i++ ] = "msn.png";
	$imgArray[ $i++ ] = "msnlogo.png";
	$imgArray[ $i++ ] = "google.png";
	$imgArray[ $i++ ] = "yahoo.png";
	
	$imgArray[ $i++ ] = "vmlogo.gif";
	$imgArray[ $i++ ] = "sagool.gif";
	$imgArray[ $i++ ] = "sproose.gif";
	$imgArray[ $i++ ] = "krugle.gif";
	$imgArray[ $i++ ] = "picsearch.gif";
	
	$imgArray[ $i++ ] = "worldmap_small.jpg";
	$imgArray[ $i++ ] = "nutch.gif";
	$imgArray[ $i++ ] = "GurujiLogo.gif";
	$imgArray[ $i++ ] = "yahoo.png";
	$imgArray[ $i++ ] = "voila.gif";
	
	$imgArray[ $i++ ] = "lycos.png";
	$imgArray[ $i++ ] = "altavista.gif";
	$imgArray[ $i++ ] = "entireweb.png";
	
	// array with String to show (instead of pictures)
	$i = 0;
	$strArray[ $i++ ] = "Google"; // famous crawler or search-tools
	$strArray[ $i++ ] = "AOL";
	$strArray[ $i++ ] = "Bing";
	$strArray[ $i++] = "Msnbot";	
	$strArray[ $i++ ] = "Msn";
	$strArray[ $i++ ] = "Googlebot";  
	$strArray[ $i++ ] = "Yahoo";
	
	$strArray[ $i++ ] = "VMBot";  	   // less famous crawler or search-tools
	$strArray[ $i++ ] = "MaSagool";
	$strArray[ $i++ ] = "sproose";  
	$strArray[ $i++ ] = "Krugle";  
	$strArray[ $i++ ] = "psbot";
	
	$strArray[ $i++ ] = "ichiro";  // less famous crawler or search-tools
	$strArray[ $i++ ] = "Nutch";
	$strArray[ $i++ ] = "Guruji";
	$strArray[ $i++ ] = "YahooSeeker";      
	$strArray[ $i++ ] = "VoilaBot";  
	
	$strArray[ $i++ ] = "Lycos";    //some old search tools 	
	$strArray[ $i++ ] = "Altavista";
	$strArray[ $i++ ] = "Entire Web";      
	
	for( $i = 0; $i < $browserNr; $i++ ) {    
		if ( stripos( $http_user_agent, $detectionArray[ $i ] ) !== false ) {
			$http_user_agent = htmlentities($http_user_agent);
			if ( $showPics && isset( $imgArray[ $i ] )){			
				$browserResultString = '<img src="'.SITEURL.'images/br/'.$imgArray[ $i ].'" alt="'.$strArray[ $i ].' - '.$http_user_agent.'" title="'.$strArray[ $i ].' - '.$http_user_agent.'" width="18" height="18" border="0" class="br_chk_img" />';
			}else{
			  $browserResultString = $strArray[ $i ];
			}			
			 
			return isset( $browserResultString ) ? $browserResultString : $http_user_agent;
		}
	}
	
	// if none is detected return default
	return $defaultBrowser;  
}

/* ------------------------------------------------------------------------- */

/** 
* Is Web Spider
*
* @desc This check whether the HTTP nuser agent is a web spider or not
* @access public
* @param string http_user_agent : user agent string from $_SERVER array	 	 
* @returns true if user-agent string contains signature of known web spider
*/
function isWebSpider( $http_user_agent ) { 
   if ( stripos( $http_user_agent, "bot") !== false || 
		stripos( $http_user_agent, "crawler") !== false ||
		stripos( $http_user_agent, 'Yahoo! Slurp' ) !== false ||           
	   (stripos( $http_user_agent, 'ichiro' ) !== false & stripos( $http_user_agent, 'goo' ) !== false) ||
	   (stripos( $http_user_agent, 'Nutch' ) !== false && ( stripos( $http_user_agent, 'bot' ) !== false ||
															stripos( $http_user_agent, 'crawl' ) !== false)) ||
		stripos( $http_user_agent, 'Speedy Spider' ) !== false || 
		stripos( $http_user_agent, 'SKIZZLE' ) !== false )
	  return true;
  else
	  return false;
}

/* ------------------------------------------------------------------------- */

/** 
* Get Known Search Engines
*
* @desc This will just return an array or known search engines
* @access public
* @param NA	
* @returns array
*/
function getKnownSearchEngines() {
  return array ('lycos.com', 'google.', 'yahoo.', 'altavista.', 'msn.com', 'picsearch', 'krugle', 'ichiro',
				'entireweb.com', 'nutch', 'WISEnutbot', 'majestic' , 'VMBot', 'Majestic12', 'entireweb.com', 
				'SKIZZLE', 'fireball.de','aol');
}
?>